module('range');

test('KC.range', function() {
	expect(6);

	var range = new KC.range(document);

	ok(range.startContainer === document);
	ok(range.startOffset === 0);
	ok(range.endContainer === document);
	ok(range.endOffset === 0);
	ok(range.collapsed === true);
	ok(range.commonAncestorContainer === document);
});

test('KC.range.setStart', function() {
	expect(6);

	var p = KC.query('#test-data-01 p');
	var strong = KC.query('#test-data-01 strong');

	var range = new KC.range(document);
	range.setStart(strong.previousSibling, 3);

	ok(range.startContainer === strong.previousSibling);
	ok(range.startOffset === 3);
	ok(range.endContainer === strong.previousSibling);
	ok(range.endOffset === 3);
	ok(range.collapsed === true);
	ok(range.commonAncestorContainer === strong.previousSibling);
});

test('KC.range.setEnd', function() {
	expect(6);

	var p = KC.query('#test-data-01 p');
	var strong = KC.query('#test-data-01 strong');

	var range = new KC.range(document);
	range.setStart(strong.previousSibling, 3);
	range.setEnd(p, 4);

	ok(range.startContainer === strong.previousSibling);
	ok(range.startOffset === 3);
	ok(range.endContainer === p);
	ok(range.endOffset === 4);
	ok(range.collapsed === false);
	ok(range.commonAncestorContainer === p);
});

test('KC.range.setStartBefore', function() {
	expect(6);

	var p = KC.query('#test-data-01 p');
	var strong = KC.query('#test-data-01 strong');

	var range = new KC.range(document);
	range.setStartBefore(strong);

	ok(range.startContainer === p);
	ok(range.startOffset === 1);
	ok(range.endContainer === p);
	ok(range.endOffset === 1);
	ok(range.collapsed === true);
	ok(range.commonAncestorContainer === p);
});

test('KC.range.setStartAfter', function() {
	expect(6);

	var p = KC.query('#test-data-01 p');
	var strong = KC.query('#test-data-01 strong');

	var range = new KC.range(document);
	range.setStartAfter(strong);

	ok(range.startContainer === p);
	ok(range.startOffset === 2);
	ok(range.endContainer === p);
	ok(range.endOffset === 2);
	ok(range.collapsed === true);
	ok(range.commonAncestorContainer === p);
});

test('KC.range.setEndBefore', function() {
	expect(6);

	var p = KC.query('#test-data-01 p');
	var strong = KC.query('#test-data-01 strong');

	var range = new KC.range(document);
	range.setStartAfter(strong);
	range.setEndBefore(strong);
	ok(range.startContainer === p);
	ok(range.startOffset === 1);
	ok(range.endContainer === p);
	ok(range.endOffset === 1);
	ok(range.collapsed === true);
	ok(range.commonAncestorContainer === p);
});

test('KC.range.setEndAfter', function() {
	expect(6);

	var p = KC.query('#test-data-01 p');
	var strong = KC.query('#test-data-01 strong');

	var range = new KC.range(document);
	range.setStartAfter(strong);
	range.setEndAfter(strong);

	ok(range.startContainer === p);
	ok(range.startOffset === 2);
	ok(range.endContainer === p);
	ok(range.endOffset === 2);
	ok(range.collapsed === true);
	ok(range.commonAncestorContainer === p);
});

test('KC.range.selectNode', function() {
	expect(6);

	var p = KC.query('#test-data-01 p');
	var strong = KC.query('#test-data-01 strong');

	var range = new KC.range(document);
	range.selectNode(strong);
	ok(range.startContainer === p);
	ok(range.startOffset === 1);
	ok(range.endContainer === p);
	ok(range.endOffset === 2);
	ok(range.collapsed === false);
	ok(range.commonAncestorContainer === p);
});

test('KC.range.selectNodeContents', function() {
	var range, strong, img, p;
	//1
	strong = KC.query('#test-data-01 strong');
	range = new KC.range(document);
	range.selectNodeContents(strong);
	ok(range.startContainer === strong);
	ok(range.startOffset === 0);
	ok(range.endContainer === strong);
	ok(range.endOffset === 1);
	ok(range.collapsed === false);
	ok(range.commonAncestorContainer === strong);
	//2
	img = KC.query('#test-data-01 img');
	range = new KC.range(document);
	range.selectNodeContents(img);
	ok(range.startContainer === img.parentNode);
	ok(range.startOffset === 3);
	ok(range.endContainer === img.parentNode);
	ok(range.endOffset === 4);
	ok(range.collapsed === false);
	ok(range.commonAncestorContainer === img.parentNode);
	//3
	strong = KC.query('#test-data-02 strong');
	range = new KC.range(document);
	range.selectNodeContents(strong);
	ok(range.startContainer === strong);
	ok(range.startOffset === 0);
	ok(range.endContainer === strong);
	ok(range.endOffset === 0);
	ok(range.collapsed === true);
	ok(range.commonAncestorContainer === strong);
	//4
	p = KC.query('#test-data-02 p');
	range = new KC.range(document);
	range.selectNodeContents(p);
	ok(range.startContainer === p);
	ok(range.startOffset === 0);
	ok(range.endContainer === p);
	ok(range.endOffset === 7);
	ok(range.collapsed === false);
	ok(range.commonAncestorContainer === p);
});

test('KC.range.collapse', function() {
	var p = KC.query('#test-data-01 p');
	var strong = KC.query('#test-data-01 strong');
	var range;
	range = new KC.range(document);
	range.setStart(p.childNodes[2], 0);
	range.setEnd(p.childNodes[4], 2);
	range.collapse(true);
	ok(range.startContainer === range.endContainer);
	ok(range.startOffset === range.endOffset);
	ok(range.collapsed === true);
	range = new KC.range(document);
	range.setStart(p.childNodes[2], 0);
	range.setEnd(p.childNodes[4], 2);
	range.collapse(false);
	ok(range.startContainer === range.endContainer);
	ok(range.startOffset === range.endOffset);
	ok(range.collapsed === true);
});

test('KC.range.compareBoundaryPoints', function() {
	var p = KC.query('#test-data-01 p');
	var strong = KC.query('#test-data-01 strong');
	var cmp, rangeA, rangeB;
	//1
	rangeA = new KC.range(document);
	rangeB = new KC.range(document);
	rangeA.setStart(p.childNodes[0], 0);
	rangeA.setEnd(p.childNodes[0], 2);
	rangeB.setStart(p.childNodes[4], 0);
	rangeB.setEnd(p.childNodes[4], 2);
	cmp = rangeA.compareBoundaryPoints(KC.START_TO_START, rangeB);
	ok(cmp === -1);
	cmp = rangeA.compareBoundaryPoints(KC.START_TO_END, rangeB);
	ok(cmp === -1);
	cmp = rangeA.compareBoundaryPoints(KC.END_TO_END, rangeB);
	ok(cmp === -1);
	cmp = rangeA.compareBoundaryPoints(KC.END_TO_START, rangeB);
	ok(cmp === -1);
	//2
	rangeA = new KC.range(document);
	rangeB = new KC.range(document);
	rangeA.setStart(p.childNodes[0], 0);
	rangeA.setEnd(p.childNodes[4], 3);
	rangeB.setStart(p.childNodes[4], 0);
	rangeB.setEnd(p.childNodes[4], 2);
	cmp = rangeA.compareBoundaryPoints(KC.START_TO_START, rangeB);
	ok(cmp === -1);
	cmp = rangeA.compareBoundaryPoints(KC.START_TO_END, rangeB);
	ok(cmp === 1);
	cmp = rangeA.compareBoundaryPoints(KC.END_TO_END, rangeB);
	ok(cmp === 1);
	cmp = rangeA.compareBoundaryPoints(KC.END_TO_START, rangeB);
	ok(cmp === -1);
	//3
	rangeA = new KC.range(document);
	rangeB = new KC.range(document);
	rangeA.setStart(p.childNodes[0], 0);
	rangeA.setEnd(p.childNodes[2], 3);
	rangeB.setStart(p.childNodes[2], 0);
	rangeB.setEnd(p.childNodes[4], 2);
	cmp = rangeA.compareBoundaryPoints(KC.START_TO_START, rangeB);
	ok(cmp === -1);
	cmp = rangeA.compareBoundaryPoints(KC.START_TO_END, rangeB);
	ok(cmp === 1);
	cmp = rangeA.compareBoundaryPoints(KC.END_TO_END, rangeB);
	ok(cmp === -1);
	cmp = rangeA.compareBoundaryPoints(KC.END_TO_START, rangeB);
	ok(cmp === -1);
	//4
	rangeA = new KC.range(document);
	rangeB = new KC.range(document);
	rangeA.selectNode(strong);
	rangeB.selectNode(p);
	cmp = rangeA.compareBoundaryPoints(KC.START_TO_START, rangeB);
	ok(cmp === 1);
	cmp = rangeA.compareBoundaryPoints(KC.START_TO_END, rangeB);
	ok(cmp === 1);
	cmp = rangeA.compareBoundaryPoints(KC.END_TO_END, rangeB);
	ok(cmp === -1);
	cmp = rangeA.compareBoundaryPoints(KC.END_TO_START, rangeB);
	ok(cmp === -1);
	//5
	rangeA = new KC.range(document);
	rangeB = new KC.range(document);
	rangeA.setStart(p.childNodes[0], 0);
	rangeA.setEnd(p.childNodes[0], 2);
	rangeB.setStart(p.childNodes[0], 0);
	rangeB.setEnd(p.childNodes[0], 2);
	cmp = rangeA.compareBoundaryPoints(KC.START_TO_START, rangeB);
	ok(cmp === 0);
	cmp = rangeA.compareBoundaryPoints(KC.START_TO_END, rangeB);
	ok(cmp === 1);
	cmp = rangeA.compareBoundaryPoints(KC.END_TO_END, rangeB);
	ok(cmp === 0);
	cmp = rangeA.compareBoundaryPoints(KC.END_TO_START, rangeB);
	ok(cmp === -1);
	//6
	rangeA = new KC.range(document);
	rangeB = new KC.range(document);
	rangeA.selectNode(strong);
	rangeB.selectNode(strong.firstChild);
	cmp = rangeA.compareBoundaryPoints(KC.START_TO_START, rangeB);
	ok(cmp === -1);
	cmp = rangeA.compareBoundaryPoints(KC.START_TO_END, rangeB);
	ok(cmp === 1);
	cmp = rangeA.compareBoundaryPoints(KC.END_TO_END, rangeB);
	ok(cmp === 1);
	cmp = rangeA.compareBoundaryPoints(KC.END_TO_START, rangeB);
	ok(cmp === -1);
	//7
	rangeA = new KC.range(document);
	rangeB = new KC.range(document);
	rangeA.selectNode(strong.firstChild);
	rangeB.setStart(strong.firstChild, 0);
	rangeB.setEnd(strong.firstChild, 2);
	cmp = rangeA.compareBoundaryPoints(KC.START_TO_START, rangeB);
	ok(cmp === -1);
	cmp = rangeA.compareBoundaryPoints(KC.START_TO_END, rangeB);
	ok(cmp === 1);
	cmp = rangeA.compareBoundaryPoints(KC.END_TO_END, rangeB);
	ok(cmp === 1);
	cmp = rangeA.compareBoundaryPoints(KC.END_TO_START, rangeB);
	ok(cmp === -1);

});

test('KC.range.cloneRange', function() {
	expect(6);
	var p = KC.query('#test-data-01 p');
	var strong = KC.query('#test-data-01 strong');

	var range = new KC.range(document);
	range.setStart(p.childNodes[2], 0);
	range.setEnd(p.childNodes[4], 2);
	var cloneRange = range.cloneRange();
	ok(range.startContainer === cloneRange.startContainer);
	ok(range.startOffset === cloneRange.startOffset);
	ok(range.endContainer === cloneRange.endContainer);
	ok(range.endOffset === cloneRange.endOffset);
	ok(range.collapsed === cloneRange.collapsed);
	ok(range.commonAncestorContainer === cloneRange.commonAncestorContainer);
});

test('KC.range.toString', function() {
	var p = KC.query('#test-data-01 p');
	var strong = KC.query('#test-data-01 strong');
	var range = new KC.range(document);
	range.setStart(p.childNodes[0], 0);
	range.setEnd(p.childNodes[4], 2);
	same(range.toString(), 'abcdefghijkxy');
});

test('KC.range.toNativeRange', function() {
	var p = KC.query('#test-data-01 p');
	var strong = KC.query('#test-data-01 strong');
	var range;
	//1
	range = new KC.range(document);
	range.selectNode(strong);
	same(KC.nativeRangeText(range.toNativeRange()), 'efg');
	//2
	range = new KC.range(document);
	range.selectNode(strong);
	range.setStart(strong.firstChild, 1);
	same(KC.nativeRangeText(range.toNativeRange()), 'fg');
	//3
	range = new KC.range(document);
	range.setStart(strong.firstChild, 1);
	range.setEnd(strong.firstChild, 2);
	same(KC.nativeRangeText(range.toNativeRange()), 'f');
	//4
	range = new KC.range(document);
	range.setStart(p.childNodes[0], 0);
	range.setEnd(p.childNodes[4], 2);
	same(KC.nativeRangeText(range.toNativeRange()), 'abcdefghijkxy');
	//5
	range = new KC.range(document);
	range.setStart(p.childNodes[4], 1);
	range.setEnd(p.childNodes[4], 2);
	same(KC.nativeRangeText(range.toNativeRange()), 'y');
	//6
	range = new KC.range(document);
	range.selectNode(p);
	same(KC.nativeRangeText(range.toNativeRange()), 'abcdefghijkxyz0123456789');
	//7
	range = new KC.range(document);
	range.selectNode(strong.firstChild);
	same(KC.nativeRangeText(range.toNativeRange()), 'efg');
	//8
	range = new KC.range(document);
	range.selectNode(strong);
	same(KC.nativeRangeText(range.toNativeRange()), 'efg');
	//9
	range = new KC.range(document);
	same(KC.nativeRangeText(range.toNativeRange()), '');
	//10
	range = new KC.range(document);
	KC.trimNode(document.body);
	range.selectNode(document.body);
	ok(KC.nativeRangeText(range.toNativeRange()).length > 100);
});

test('KC.toRange', function() {
	var div = KC.query('#test-data-01');
	var p = KC.query('#test-data-01 p');
	var strong = KC.query('#test-data-01 strong');
	KC.trimNode(div);
	var nativeRange, range;
	//1
	nativeRange = KC.createNativeRange(document, strong.firstChild, 1, strong.firstChild, 2);
	range = KC.toRange(nativeRange);
	console.log(range);
	return;
	same(range.toString(), 'f');
	//2
	nativeRange = KC.createNativeRange(document, div, 0, div, 1);
	range = KC.toRange(nativeRange);
	same(range.toString(), 'abcdefghijkxyz0123456789');
});
